local shicai = fk.CreateSkill {
  name = "kl__shicai",
}

Fk:addQmlMark{
  name = "kl__shicai",
  how_to_show = function(name, value)
    if type(value) == "string" then
      return Fk:translate(value)
    elseif type(value) == "table" then
      return tostring(#value)
    end
    return " "
  end,
  qml_path = "packages/konglong/qml/HxDetailBox"
}

shicai:addEffect("active", {
  anim_type = "control",
  prompt = function(self, player, selected_cards, selected)
    local x = player:usedSkillTimes(shicai.name) + 1
    return "#kl__shicai:::"..x
  end,
  min_card_num = function (self, player)
    return player:usedSkillTimes(shicai.name) + 1
  end,
  target_num = 0,
  can_use = function(self, player)
    local x = player:usedSkillTimes(shicai.name) + 1
    return #player:getCardIds("he") >= x
  end,
  card_filter = function(self, player, to_select, selected)
    local x = player:usedSkillTimes(shicai.name) + 1
    return  #selected < x
  end,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    local tocards = effect.cards
    player:addToPile(shicai.name, effect.cards, true, shicai.name, player)
    local mark = player:getTableMark("@$kl__guzhen") or {}
    if player.dead or #mark == 0 then return end
    if #mark > 0 then
      local use = room:askToUseVirtualCard(player, {
        name = mark,
        skill_name = shicai.name,
        prompt = "#kl__shicai-ask",
        extra_data = {
          bypass_times = true,
          expand_pile = mark,
        },
        cancelable = false,
        skip = true,
      })
      if use then
        if use.card.type ~= Card.TypeEquip then
          local card = Fk:cloneCard(use.card.name)
          card.skillName = shicai.name
          if use.card.sub_type == Card.SubtypeDelayedTrick then
            room:sendLog{
              type = "视为使用的牌 %arg 是没有实体牌的延时锦囊牌，故未实际使用！",
              arg = card:toLogString(),
            }
          else
            room:useCard{
              card = card,
              from = player,
              tos = use.tos,
              extraUse = true,
            }
          end
        else
          if not player:getAvailableEquipSlots(use.card.sub_type) then return end
          local cardid = room:askToChooseCardsAndChoice(player,{
            cards = table.filter(player:getPile(shicai.name), function(id) return table.contains(tocards, id) end),
            min_num = 1,
            max_num = 1,
            choices = {"OK"},
            skill_name = shicai.name,
            prompt = "适才：请选择一张牌将其当作“"..Fk:translate(use.card.name).."”置入你的装备区",
          })[1]
          local card = Fk:getCardById(cardid)
          local view_card = Fk:cloneCard(use.card.name, card.suit, card.number)
          local mark2 = #player:getTableMark("@[kl__shicai]kl__shicai_viewname") or 0
          room:setCardMark(card, "kl__shicai_cardname", use.card.name)  --技能标记，同时也用来锁视为
          room:setCardMark(card, shicai.name, mark2)  --顺序标记，用于清除显示
          Fk:filterCard(cardid, player)  --先过一遍锁视为
          --发个战报：
          room:sendLog{
            type = "%from 将 %arg 当作 %arg2 置入了装备区",
            from = player.id,
            arg = card:toLogString(),
            arg2 = view_card:toLogString(),
          }
          --宝宝标记
          room:addTableMark(player,"@[kl__shicai]kl__shicai_viewname",mark2.."."..card:toLogString().."视为"..view_card:toLogString())
          room:moveCardIntoEquip(player, cardid, shicai.name, true, player)  --“合法”的移进装备区
        end
      end
    end
  end
})

shicai:addEffect("filter", {
  card_filter = function(self, to_select, player, isJudgeEvent)
    return to_select:getMark("kl__shicai_cardname") ~= 0
  end,
  view_as = function(self, player, to_select)
    return Fk:cloneCard(to_select:getMark("kl__shicai_cardname"), to_select.suit, to_select.number)
  end,
})

shicai:addEffect(fk.AfterCardsMove, {
  mute = true,
  is_delay_effect = true,
  can_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.toArea ~= Card.PlayerEquip and move.toArea ~= Card.Processing then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip and Fk:getCardById(info.cardId, true):getMark("kl__shicai_cardname") ~= 0 then
            return player:hasSkill(shicai.name,true,true)
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.toArea ~= Card.PlayerEquip and move.toArea ~= Card.Processing then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip then
            local mark = player:getMark("@[kl__shicai]kl__shicai_viewname") or {}
            local num = Fk:getCardById(info.cardId, true):getMark(shicai.name)
            for _, v in ipairs(mark) do
              if v:startsWith(""..num.."") then
                table.removeOne(mark, v)
              end
            end
            if not player.dead then
              player.room:setPlayerMark(player,"@[kl__shicai]kl__shicai_viewname", mark)
            end
            player.room:setCardMark(Fk:getCardById(info.cardId, true), "kl__shicai_cardname", 0)
            player.room:setCardMark(Fk:getCardById(info.cardId, true), shicai.name, 0)
          end
        end
      end
    end
  end,
})

shicai:addEffect(fk.TurnEnd, {
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(shicai.name) and player:usedSkillTimes(shicai.name) > 0 and #player:getPile(shicai.name) > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = player:usedSkillTimes(shicai.name) - 1
    local cards = room:askToChooseCardsAndChoice(player,{
      cards = player:getPile(shicai.name),
      min_num = x,
      max_num = x,
      choices = {"OK"},
      skill_name = shicai.name,
      prompt = "请选择"..x.."张牌获得",
    })
    room:moveCardTo(cards, Player.Hand, player,fk.ReasonPrey, shicai.name, nil, true, player)
  end,
})


return shicai